package com.zrrd.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zrrd.entity.Person;
import com.zrrd.service.PersonService;
import com.zrrd.utils.PageQuery;
import com.zrrd.utils.ResponseResult;
import com.zrrd.utils.ResponseStatusCode;
import com.zrrd.utils.query.AgeQuery;
import com.zrrd.utils.query.LoginQuery;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ZhangYK
 * @version 8.0
 * @title PersonController
 * @description
 * @create 2024/3/23 21:27
 */
@RestController
@RequestMapping("/persons")
public class PersonController {
    @Resource
    PersonService personService;

    @GetMapping
    @ResponseBody
    public ResponseResult<List<Person>> select(){
        return ResponseResult.queryResponse(personService.list());
    }

    @GetMapping("/{id}")
    @ResponseBody
    public ResponseResult<Person> selectId(@PathVariable Integer id){
        return new ResponseResult<>(20010,personService.getById(id),"id查询成功");
    }

    @PostMapping
    @ResponseBody
    public ResponseResult<Boolean> add(@RequestBody Person person){
        return ResponseResult.addResponse(personService.save(person));
    }

    @PutMapping
    @ResponseBody
    public ResponseResult<Boolean> update(@RequestBody Person person){
        return ResponseResult.updateResponse(personService.updateById(person));
    }

    @DeleteMapping("/{id}")
    @ResponseBody
    public ResponseResult<Boolean> delete(@PathVariable Integer id){
        return ResponseResult.removeResponse(personService.removeById(id));
    }

    @PostMapping("/page")
    @ResponseBody
    public ResponseResult queryPage(@RequestBody PageQuery pageQuery){
        //1.创建一个Ipage分页表，设置分页参数
        IPage<Person> page = new Page<>(pageQuery.getCurrentPage(), pageQuery.getPageSize());
        page = personService.page(page);

        return ResponseResult.queryResponse(page.getRecords());
    }

    @PostMapping("/queryAllName")
    @ResponseBody
    public ResponseResult queryAllName(){
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        lqw.select(Person::getName);//指定字段name
        //相当于SQL语句
        List<Person> list = personService.list(lqw);

        //如果想要抽取person集合（List《Person》中的所有姓名）
        //方式一
        List<String> list1 = new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            Person person = list.get(i);
            list1.add(person.getName());
        }
        return ResponseResult.queryResponse(list1);
    }

    /**
     * 查询总记录条数
     * @return
     */
    @PostMapping("/queryCount")
    @ResponseBody
    public ResponseResult queryCount(){
        //方式一
        int count = personService.count();
        return ResponseResult.queryResponse(count);
    }

    /**
     * 分组查询：按照年龄进行分组，查询每组年龄人数
     * @return
     */
    @PostMapping("/queryGroupBy")
    @ResponseBody
    public ResponseResult queryGroupBy(){
        //fangshier
        QueryWrapper<Person> qw = new QueryWrapper<>();
        // select count(*) count,age from person
        qw.select("count(*) count,age");
        qw.groupBy("age");
        List<Map<String, Object>> maps = personService.listMaps(qw);
        return ResponseResult.queryResponse(maps);
    }

    @PostMapping("/login")
    @ResponseBody
    public ResponseResult login(@RequestBody LoginQuery loginQuery){
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        lqw.eq(loginQuery.getUsername()!=null,Person::getUsername,loginQuery.getUsername());
        Person person = personService.getOne(lqw);
        if (person==null){
            return new ResponseResult<>(ResponseStatusCode.DEFAULT_ERR.getCode(),null,"登录失败");
        }else if (!(person.getPassword().equals(loginQuery.getPassword()))){
            return new ResponseResult<>(ResponseStatusCode.DEFAULT_ERR.getCode(),null,"登录失败");
        }

        person.setUsername("");
        person.setPassword("");
        return new ResponseResult<>(ResponseStatusCode.DEFAULT_OK.getCode(),person,"登录成功");
    }

    @PostMapping("/queryByCond03")
    @ResponseBody
    public ResponseResult queryByCond03(@RequestBody AgeQuery ageQuery){
        LambdaQueryWrapper<Person> lqw = new LambdaQueryWrapper<>();
        lqw.between(ageQuery!=null,Person::getAge,ageQuery.getMinAge(),ageQuery.getMaxAge());
        List<Person> list = personService.list(lqw);
        return ResponseResult.queryResponse(list);
    }

    @PostMapping("/deleteByIds")
    @ResponseBody
    public ResponseResult deleteByIds(@RequestBody List<Integer> list){
        boolean b = personService.removeByIds(list);
        return ResponseResult.removeResponse(b);
    }

    @PostMapping("/queryByIds")
    @ResponseBody
    public ResponseResult queryByIds(@RequestBody List<Integer> list){
        List listByIds = personService.listByIds(list);
        return ResponseResult.queryResponse(listByIds);
    }

    @PutMapping("/update01")
    @ResponseBody
    public ResponseResult update01(@RequestBody Person person){
        Person person1 = personService.getById(person.getId());
        person.setVersion(person1.getVersion());
        boolean update = personService.updateById(person);
        return ResponseResult.updateResponse(update);
    }

}
